{extend name="public:base" /}
{block name="title"}后台管理-权限列表{/block}
{block name="body"}
  <div class="x-nav">
    <span class="layui-breadcrumb">
        <a href="{:url('admin/index/index')}">首页</a>
        <a>
            <cite>权限列表</cite></a>
    </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
    </a>
</div>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                    <form class="layui-form layui-col-space5">
                        <div class="layui-input-inline layui-show-xs-block">
                            <button class="layui-btn"  type="button" lay-submit="" lay-filter="sreach" onclick="xadmin.open('添加权限','{:url('admin/rule/add')}',600,400)"><i class="layui-icon"></i>添加权限</button>
                        </div>
                    </form>
                    <hr>
                </div>
                
                <div class="layui-card-body ">
                    <table class="layui-table layui-form">
                      <thead>
                        <tr>
                          <th width="70">序号</th>
                          <th>权限名称</th>
                          <th>连接名</th>
                          <th width="50">排序</th>
                          <th width="250">操作</th>
                      </thead>
                      <tbody class="x-cate">
                        {volist name="data" id="v" key="k"}
                        <tr cate-id='{$v["id"]}' fid='{$v["pid"]}' ><!-- cate-id是ID  fid是父级ID   -->
                            <td>
                                {$k}
                            </td>
                            <td>
                              <!-- 父级自己分类 -->
                                {for start="0" end="$v['level']"}
                                    &nbsp;&nbsp;&nbsp;&nbsp;
                                {/for}
                                {if condition="$v['child']"}
                                    <i class="layui-icon x-show iconfont" status='true'>&#xe623;</i>
                                {else/}
                                  {if condition="$v['level'] neq 0"}
                                    |--
                                  {/if}
                                {/if}
                                {$v['name']}
                            </td>
                            <td>
                                {$v['mac']}
                            </td>
                            <td>
                                <input type="text" class="layui-input x-sort" name="sort[]" id="{$v['id']}" value="{$v['sort']}">
                            </td>
                            <td class="td-manage">
                                <button class="layui-btn layui-btn layui-btn-xs"  onclick="xadmin.open('修改{$v['name']}权限','{:url('admin/rule/edit',array('id'=>$v['id']))}',600,400)" >
                                  <i class="layui-icon">&#xe642;</i>编辑
                                </button>
                                {lt name="$v['level']" value="2"}
                                  <button class="layui-btn layui-btn-warm layui-btn-xs"  onclick="xadmin.open('添加子栏目','{:url('admin/rule/add',array('pid'=>$v['id']))}',600,400)" >
                                    <i class="layui-icon">&#xe642;</i>添加子栏目
                                  </button>
                                {/lt}
                                <button class="layui-btn-danger layui-btn layui-btn-xs"  onclick="member_del(this,'{$v['id']}')" href="javascript:;" >
                                  <i class="layui-icon">&#xe640;</i>删除
                                </button>
                            </td>
                        </tr>
                        {/volist}
                      </tbody>
                    </table>
                </div>
                <div class="layui-card-body ">
                    <div class="page">
                        <form class="layui-form layui-col-space5">
                          <div class="layui-input-inline layui-show-xs-block">
                              <button class="layui-btn"  type="button" lay-submit="" onclick="saveSort()" lay-filter="sreach" ><i class="iconfont">&#xea97;</i>保存</button>
                          </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{/block}

{block name="js"}
  <script>
    layui.use(['form'], function(){
      form = layui.form;
      
    });

     /*用户-删除*/
    function member_del(obj,id){
        layer.confirm('确认要删除吗？',function(index){
          // console.log(id);
            //发异步删除数据
            let data = {
              'id':id
            };
            $.post("{:url('admin/rule/del')}",data,function(e){
              if(e.code == 200){
                  $(obj).parents("tr").remove();
                  xadmin.father_reload();
              }else{
                  layer.msg(e.msg,{icon:2,time:1000});
              }
              return true;
            });
        });
    }

    // 分类展开收起的分类的逻辑
    // 
    $(function(){
      $("tbody.x-cate tr[fid!='0']").hide();
      // 栏目多级显示效果
      $('.x-show').click(function () {
          if($(this).attr('status')=='true'){
              $(this).html('&#xe625;'); 
              $(this).attr('status','false');
              cateId = $(this).parents('tr').attr('cate-id');
              $("tbody tr[fid="+cateId+"]").show();
         }else{
              cateIds = [];
              $(this).html('&#xe623;');
              $(this).attr('status','true');
              cateId = $(this).parents('tr').attr('cate-id');
              getCateId(cateId);
              for (var i in cateIds) {
                  $("tbody tr[cate-id="+cateIds[i]+"]").hide().find('.x-show').html('&#xe623;').attr('status','true');
              }
         }
      })
    })

    var cateIds = [];
    function getCateId(cateId) {
        $("tbody tr[fid="+cateId+"]").each(function(index, el) {
            id = $(el).attr('cate-id');
            cateIds.push(id);
            getCateId(id);
        });
    }


    //排序编号保存
    function saveSort(){
      let sortarr = $('input[name="sort[]"]');
      let data = {};
      let temp;
      for (var i =  0; sortarr.length > i; i++) {
          temp = $(sortarr[i]);
          data[i] = {};
          data[i]['id'] = temp[0].id;
          data[i]['sort'] = temp.val();
      }
      data  = {
        'data':JSON.stringify(data)
      };
      $.post("{:url('admin/rule/saveSort')}",data,function(e){
        if(e.code == 200){
            layer.msg(e.msg,{icon:1,time:1000});
            location.reload();
            return true;
        }else{
            layer.msg(e.msg,{icon:2,time:1000});
        }
        
      });
    }

  </script>
{/block}